package mosync.mobilelualiveditor;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.event.*;
import java.beans.*; //property change stuff

public class PropertyUI extends Container
{
	private static final long serialVersionUID = 1L;
	
	private final JFrame mParentFrame;

	Component makeControllerLayout(PropertyController pc)
	{
		// Left layout
		Container left = new Container();
		JComponent controller = pc.createComponent();
		left.setLayout(new GridLayout(2, 1));
		JLabel nameLabel = new JLabel(pc.getName());
		nameLabel.setBackground(Color.RED);
		left.add(nameLabel);
		left.add(controller);

		// Right layout
		Container right = new Container();
		right.setLayout(new GridLayout(2, 1));
		JButton edit = new JButton("Edit");
		JButton delete = new JButton("Delete");
		right.add(edit);
		right.add(delete);

		// Main layout
		Container main = new Container();
		GridBagLayout gbl = new GridBagLayout();
		main.setLayout(gbl);

		GridBagConstraints rightConstraints = new GridBagConstraints();
		rightConstraints.fill = GridBagConstraints.HORIZONTAL;
		rightConstraints.anchor = GridBagConstraints.NORTHWEST;
		rightConstraints.weightx = 0.0;
		rightConstraints.gridwidth = GridBagConstraints.REMAINDER;
		gbl.setConstraints(right, rightConstraints);

		GridBagConstraints labelConstraints = (GridBagConstraints) rightConstraints
			.clone();
		// Give these as little space as necessary
		labelConstraints.weightx = 1.0;
		labelConstraints.gridwidth = 1;
		gbl.setConstraints(left, labelConstraints);

		main.add(left);
		main.add(right);
		main.setMaximumSize(new Dimension(800, 60));

		return main;
	}

	class PropertyDialog extends JDialog
	{
		private static final long serialVersionUID = 1L;
		
		public PropertyDialog(JFrame parentFrame, final PropertyController pc)
		{
			super(parentFrame, "", Dialog.ModalityType.DOCUMENT_MODAL);
			setSize(500, 500);

			this.mPropertyController = pc;

			mCodeArea.setSize(300, 300);
			mCodeArea.setMinimumSize(new Dimension(300, 300));
			// codeArea.setText(pc.getTemplate());
			mTemplateLabel = new JLabel(pc.getCallbackTemplateHeader(pc
				.getName()));
			Object[] array = { mNameString, mNameField, new JSeparator(),
				mCodeString, mTemplateLabel, mCodeArea, "end" };

			// Create an array specifying the number of dialog buttons
			// and their text.
			Object[] options = { mOkString, mCancelString };

			// Create the JOptionPane.
			mOptionPane = new JOptionPane(array, JOptionPane.QUESTION_MESSAGE,
				JOptionPane.YES_NO_OPTION, null, options, options[0]);
			setContentPane(mOptionPane);

			final JDialog dialog = this;

			mNameField.getDocument().addDocumentListener(new DocumentListener()
			{
				public void changedUpdate(DocumentEvent documentEvent)
				{
					updateLabel();
				}

				public void insertUpdate(DocumentEvent documentEvent)
				{
					updateLabel();
				}

				public void removeUpdate(DocumentEvent documentEvent)
				{
					updateLabel();
				}

				void updateLabel()
				{
					mTemplateLabel.setText(pc
						.getCallbackTemplateHeader(mNameField.getText()));

				}
			});

			mOptionPane.addPropertyChangeListener(new PropertyChangeListener()
			{
				public void propertyChange(PropertyChangeEvent e)
				{
					String prop = e.getPropertyName();

					if (isVisible()
						&& (e.getSource() == mOptionPane)
						&& (JOptionPane.VALUE_PROPERTY.equals(prop) || JOptionPane.INPUT_VALUE_PROPERTY
							.equals(prop)))
					{
						Object value = mOptionPane.getValue();
						if (value == JOptionPane.UNINITIALIZED_VALUE)
						{
							// ignore reset
							return;
						}

						mOptionPane.setValue(JOptionPane.UNINITIALIZED_VALUE);

						if (value.equals(mOkString))
						{
							mReturnCode = 1;
							dialog.setVisible(false);
						}
						else if (value.equals(mCancelString))
						{
							mReturnCode = 0;
							dialog.setVisible(false);
						}
					}
				}
			});
		}

		public int getReturnCode()
		{
			return mReturnCode;
		}

		public String getNameString()
		{
			return mNameField.getText();
		}

		public String getCodeArea()
		{
			return mCodeArea.getText();
		}

		private int mReturnCode = 0;

		private JTextField mNameField = new JTextField();
		private JTextArea mCodeArea = new JTextArea();

		private JLabel mTemplateLabel;
		private String mOkString = "Ok";
		private String mCancelString = "Cancel";
		private String mNameString = "Name:";
		private String mCodeString = "Code:";
		private JOptionPane mOptionPane;
		// TODO: mPropertyController is not used. Remove it if not needed.
		@SuppressWarnings("unused")
		private PropertyController mPropertyController;
	}

	void showPropertyDialog(PropertyController pc)
	{
		// Create an array of the text and components to be displayed.

	}

	PropertyUI(JFrame parent)
	{
		mParentFrame = parent;

		String[] labels = { "slider", "checkbox", "textfield" };
		final JComboBox propertyList = new JComboBox(labels);
		final JButton createPropertyButton = new JButton("Create controller");

		final JToolBar propertyToolBar = new JToolBar("Still draggable");

		// JScrollPane scrollPane = new JScrollPane();
		// propertyCreateContainer.add(propertyList);
		// propertyCreateContainer.add(createPropertyButton);
		propertyToolBar.add(propertyList, BorderLayout.PAGE_START);
		propertyToolBar.add(createPropertyButton, BorderLayout.CENTER);
		propertyToolBar.setSize(12412412, 40);

		final Container properties = new Container();
		JScrollPane scrollPane2 = new JScrollPane(properties);
		properties.setLayout(new BoxLayout(properties, BoxLayout.Y_AXIS));
		scrollPane2.setSize(1251251, 125125);

		final Container contentPane = this; // getContentPane();
		contentPane.setLayout(new BorderLayout());

		// Create a split pane with the two scroll panes in it.
		// final JSplitPane splitPane = new
		// JSplitPane(JSplitPane.VERTICAL_SPLIT, scrollPane, scrollPane2);
		// splitPane.setOneTouchExpandable(true);
		// splitPane.setDividerLocation(150);

		// Provide minimum sizes for the two components in the split pane
		Dimension minimumSize = new Dimension(100, 50);
		// scrollPane.setMinimumSize(minimumSize);
		scrollPane2.setMinimumSize(minimumSize);

		contentPane.add(propertyToolBar, BorderLayout.PAGE_START);
		contentPane.add(scrollPane2, BorderLayout.CENTER);

		this.setSize(400, 800);
		// pack();
		setVisible(true);

		createPropertyButton.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent evt)
			{
				System.out
					.println("Selected " + propertyList.getSelectedItem());

				PropertyController c = null;

				switch (propertyList.getSelectedIndex())
				{
					case 0:
						c = new SliderController("bajs");
						break;
					case 1:
						c = new CheckboxController("bajs");
						break;
					case 2:
						c = new TextfieldController("bajs");
						break;

					default:
				}

				// } else {
				// c = new ColorPickerController("" + evt.getLastIndex());
				// }

				PropertyDialog pd = new PropertyDialog(mParentFrame, c);
				pd.setVisible(true);
				if (pd.getReturnCode() == 0)
					return;
				c.setName(pd.getNameString());
				properties.add(makeControllerLayout(c));

				String callbackCode = c.getCallbackTemplateHeader(c.getName())
					+ "\n" + pd.getCodeArea() + "\nend\n";
				Evaluator.getSingleton().evaluate(callbackCode);

				// properties.add(new JLabel("kiss"));
				contentPane.validate();
			}
		});
	}
}
